package ua.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import ua.form.UserForm;
import ua.model.RoleListBean;
import ua.model.UserListBean;

public class UserAction extends DispatchAction{

	
	public ActionForward addUser(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) {

			UserForm userForm = (UserForm) form;
			userForm.setRoles(RoleListBean.getRoles());
			return mapping.findForward("showAdd");		
	}
	
	public ActionForward saveUser(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) {
			
		    UserForm userForm = (UserForm) form;
			userForm.save();	    
			return mapping.findForward("showList");		
	}
	
	public ActionForward editUser(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) {
			
		    UserForm userForm = (UserForm) form;
		    String username = request.getParameter("username");  
		    
		    userForm.setUser(UserListBean.getUserByName(username));
		    userForm.setRoles(RoleListBean.getRoles());
			return mapping.findForward("showEdit");		
	}
		

	public ActionForward deleteUser(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) {

	        UserForm userForm = (UserForm) form;
	        userForm.setId(request.getParameter("username"));
	        userForm.getUser().delete();
	    
			return mapping.findForward("showList");	

	}
		
}
